home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / Examples / GUSIFileTest.c < prev    next >
Text File  |  1995-06-03  |  11KB  |  455 lines

  1. /*********************************************************************
  2. File        :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSIFileTest    -    Test plain files
  4. Author    :    Matthias Neeracher <neeri@iis.ethz.ch>
  5. Language    :    MPW C
  6.  
  7. $Log: GUSIFileTest.c,v $
  8. Revision 1.2  1994/12/31  01:10:41  neeri
  9. ANSIfy.
  10. Test FSSpec encoding.
  11.  
  12. Revision 1.1  1994/02/25  02:46:54  neeri
  13. Initial revision
  14.  
  15. Revision 0.9  1993/07/29  00:00:00  neeri
  16. scandir
  17.  
  18. Revision 0.8  1993/07/18  00:00:00  neeri
  19. dirent -> struct dirent
  20.  
  21. Revision 0.7  1992/12/20  00:00:00  neeri
  22. Allow defaults for choose()
  23.  
  24. Revision 0.6  1992/12/08  00:00:00  neeri
  25. Pwd()
  26.  
  27. Revision 0.5  1992/10/27  00:00:00  neeri
  28. Forgot to adapt it to dirent.h
  29.  
  30. Revision 0.4  1992/09/07  00:00:00  neeri
  31. RdLink()
  32.  
  33. Revision 0.3  1992/07/25  00:00:00  neeri
  34. Isolated testing gear in GUSITest
  35.  
  36. Revision 0.2  1992/07/13  00:00:00  neeri
  37. Test choose()
  38.  
  39. Revision 0.1  1992/06/14  00:00:00  neeri
  40. More tests
  41.  
  42. *********************************************************************/
  43.  
  44. #include "GUSI.h"
  45. #include "GUSITest.h"
  46. #include "TFileSpec.h"
  47. #include "TFileGlob.h"
  48. #include <Types.h>
  49. #include <dirent.h>
  50. #include <stdio.h>
  51. #include <fcntl.h>
  52. #include <sys/errno.h>
  53. #include <stdlib.h>
  54. #include <sys/unistd.h>
  55.  
  56. void Stat(char ch1, char ch2, const char * cmd)
  57. {
  58.     int             res;
  59.     struct stat    statbuf;
  60.     char            filename[80];
  61.  
  62.     if (sscanf(cmd, "%s", filename) != 1)
  63.         Usage(ch1, ch2);
  64.     else {
  65.         if (ch2 == 'l') {
  66.             cmd    =    "lstat";
  67.             res     =    lstat(filename, &statbuf);
  68.         } else {
  69.             cmd     =     "stat";
  70.             res     =    stat(filename, &statbuf);
  71.         }
  72.     
  73.         if (res)    {
  74.             printf("# %s(\"%s\") returned error %s\n", cmd, filename, Explain());
  75.         } else {
  76.             printf("# %s(\"%s\") =\n", cmd, filename);
  77.             DUMP(statbuf.st_dev,d);
  78.             DUMP(statbuf.st_ino,d);
  79.             DUMP(statbuf.st_mode,o);
  80.             DUMP(statbuf.st_nlink,d);
  81.             DUMP(statbuf.st_uid,d);
  82.             DUMP(statbuf.st_gid,d);
  83.             DUMP(statbuf.st_rdev,d);
  84.             DUMP(statbuf.st_size,d);
  85.             DUMP(statbuf.st_atime,u);
  86.             DUMP(statbuf.st_mtime,u);
  87.             DUMP(statbuf.st_ctime,u);
  88.             DUMP(statbuf.st_blksize,d);
  89.             DUMP(statbuf.st_blocks,d);
  90.         }    
  91.         Where();
  92.     }
  93. }
  94.  
  95. void ChDir(char ch1, char ch2, const char * cmd)
  96. {
  97.     char            directory[80];
  98.  
  99.     if (sscanf(cmd, "%s", directory) != 1)
  100.         Usage(ch1, ch2);
  101.     else if (chdir(directory))    {
  102.         printf("# chdir(\"%s\") returned error %s\n", directory, Explain());
  103.         Where();
  104.     }
  105. }
  106.  
  107. void MkDir(char ch1, char ch2, const char * cmd)
  108. {
  109.     char            directory[80];
  110.  
  111.     if (sscanf(cmd, "%s", directory) != 1)
  112.         Usage(ch1, ch2);
  113.     else if (mkdir(directory))    {
  114.         printf("# mkdir(\"%s\") returned error %s\n", directory, Explain());
  115.         Where();
  116.     }
  117. }
  118.  
  119. void RmDir(char ch1, char ch2, const char * cmd)
  120. {
  121.     char            directory[80];
  122.  
  123.     if (sscanf(cmd, "%s", directory) != 1)
  124.         Usage(ch1, ch2);
  125.     else if (rmdir(directory))    {
  126.         printf("# rmdir(\"%s\") returned error %s\n", directory, Explain());
  127.         Where();
  128.     }
  129. }
  130.  
  131. void List(char ch1, char ch2, const char * cmd)
  132. {
  133.     int                    count;
  134.     int                    i;
  135.     struct dirent **    entries;
  136.     char *                dirend;
  137.     char                    directory[80];
  138.     struct stat            statbuf;
  139.  
  140.     if (sscanf(cmd, "%s", directory) != 1)
  141.         strcpy(directory, ":");
  142.     
  143.     if (ch2 == 'l' && !strchr(directory, ':')) {
  144.         directory[0] = ':';
  145.         sscanf(cmd, "%s", directory+1);
  146.     }
  147.     
  148.     if ((count = scandir(directory, &entries, nil, nil)) < 0) {
  149.         printf("# scandir(\"%s\") returned error %s\n", directory, Explain());
  150.         goto error;
  151.     }
  152.     
  153.     printf("# directory \"%s\" =\n", directory);
  154.     
  155.     dirend = directory + strlen(directory);
  156.     if (dirend[-1] != ':')
  157.         *dirend++ = ':';
  158.     
  159.     for (i = 0; i < count; ++i)
  160.         if (ch2 == 's')
  161.             printf("#    %s\n", entries[i]->d_name);
  162.         else {
  163.             strcpy(dirend, entries[i]->d_name);
  164.             
  165.             if (lstat(directory, &statbuf)) 
  166.                 printf("# lstat(\"%s\") returned error %s\n", entries[i]->d_name, Explain());
  167.             else
  168.                 printf("#    %c %7d %s\n", 
  169.                     (statbuf.st_mode & S_IFMT) == S_IFREG ? 'F' :
  170.                     (statbuf.st_mode & S_IFMT) == S_IFDIR ? 'D' :
  171.                     (statbuf.st_mode & S_IFMT) == S_IFLNK ? 'L' : '?',
  172.                     statbuf.st_size,
  173.                     entries[i]->d_name);
  174.         }
  175.         
  176. error:
  177.     Where();
  178. }
  179.  
  180. void Type(char ch1, char ch2, const char * cmd)
  181. {
  182.     FILE *         fl;
  183.     char            line[500];
  184.     char            filename[80];
  185.  
  186.     if (sscanf(cmd, "%s", filename) != 1)
  187.         Usage(ch1, ch2);
  188.     else {
  189.         fl = fopen(filename, "r");
  190.         
  191.         if (!fl)
  192.             printf("# open(\"%s\") returned error %s\n", filename, Explain());
  193.         else {
  194.             printf("# \"%s\" =\n", filename);
  195.             while (fgets(line, 500, fl))
  196.                 fputs(line, stdout);
  197.  
  198.             fclose(fl);
  199.         }
  200.         
  201.         
  202.         Where();
  203.     }
  204. }
  205.  
  206. void Encode(char ch1, char ch2, const char * cmd)
  207. {
  208.     OSErr            err;
  209.     char            filename[80];
  210.  
  211.     if (sscanf(cmd, "%s", filename) != 1)
  212.         Usage(ch1, ch2);
  213.     else {
  214.         FSSpec spec;
  215.         
  216.         if (err = Path2FSSpec(filename, &spec))
  217.             fprintf(stderr, "Path2FSSpec(%s) returned error %d\n", filename, err);
  218.         else
  219.             fprintf(stderr, "%s -> %s\n", filename, FSp2Encoding(&spec));
  220.     }
  221. }
  222.  
  223. void Glob(char ch1, char ch2, const char * cmd)
  224. {
  225.     OSErr            err;
  226.     FileGlobRef    glob;
  227.     char            filename[80];
  228.  
  229.     if (sscanf(cmd, "%s", filename) != 1)
  230.         Usage(ch1, ch2);
  231.     else if (!(glob = NewFileGlob(filename)))
  232.         fprintf(stderr, "NewFileGlob(%s) failed.\n", filename);
  233.     else {
  234.         FSSpec spec;
  235.             
  236.         fprintf(stderr, "Glob(%s) matched:\n", filename);
  237.         while (FileGlob2FSSpec(glob, &spec)) {
  238.             fprintf(stderr, "   %s\n", FSp2FullPath(&spec));
  239.             NextFileGlob(glob);
  240.         }
  241.         DisposeFileGlob(glob);
  242.     }
  243. }
  244.  
  245. void Edit(char ch1, char ch2, const char * cmd)
  246. {
  247.     FILE *         fl;
  248.     char            line[500];
  249.     char            filename[80];
  250.  
  251.     if (sscanf(cmd, "%s", filename) != 1)
  252.         Usage(ch1, ch2);
  253.     else {
  254.         fl = fopen(filename, "w");
  255.         
  256.         if (!fl)
  257.             printf("# open(\"%s\") returned error %s\n", filename, Explain());
  258.         else    {
  259.             printf("# Enter \"%s\", terminate with \".\"\n", filename);
  260.             while (fgets(line, 500, stdin))
  261.                 if (strcmp(line, ".\n"))
  262.                     fputs(line, fl);
  263.                 else 
  264.                     break;
  265.         
  266.             fclose(fl);
  267.         }
  268.     }
  269. }
  270.  
  271. void Rm(char ch1, char ch2, const char * cmd)
  272. {
  273.     char            filename[80];
  274.  
  275.     if (sscanf(cmd, "%s", filename) != 1)
  276.         Usage(ch1, ch2);
  277.     else if (remove(filename))    {
  278.         printf("# remove(\"%s\") returned error %s\n", filename, Explain());
  279.         Where();
  280.     }
  281. }
  282.  
  283. void Mv(char ch1, char ch2, const char * cmd)
  284. {
  285.     struct stat    statbuf;
  286.     char            oldfilename[80];
  287.     char            newfilename[80];
  288.  
  289.     if (sscanf(cmd, "%s %s", oldfilename, newfilename) != 2)
  290.         Usage(ch1, ch2);
  291.     else {
  292.         if (!stat(newfilename, &statbuf) && (statbuf.st_mode & S_IFMT) == S_IFDIR) {
  293.             char *    fn;
  294.             char *     next;
  295.             int         len     =     strlen(newfilename);
  296.             
  297.             /* Extract file name part from oldfilename */
  298.             for (fn = oldfilename; (next = strchr(fn, ':')) && next[1]; fn = next+1);
  299.             
  300.             if (!strchr(newfilename, ':')) {    /* Prepend ':' if necessary */
  301.                 newfilename[0] = ':';
  302.                 sscanf(cmd, "%s %s", oldfilename, newfilename+1);
  303.                 ++len;
  304.             }
  305.                 
  306.             if (newfilename[len-1] != ':')
  307.                 newfilename[len++-1] = ':';
  308.                 
  309.             strcpy(newfilename+len, fn);
  310.         }
  311.         
  312.         if (rename(oldfilename, newfilename))    {
  313.             printf("# rename(\"%s\", \"%s\") returned error %s\n", oldfilename, newfilename, Explain());
  314.             Where();
  315.         }
  316.     }
  317. }
  318.  
  319. void Link(char ch1, char ch2, const char * cmd)
  320. {
  321.     char            oldfilename[80];
  322.     char            newfilename[80];
  323.  
  324.     if (sscanf(cmd, "%s %s", oldfilename, newfilename) != 2)
  325.         Usage(ch1, ch2);
  326.     else {        
  327.         if (symlink(oldfilename, newfilename))    {
  328.             printf("# symlink(\"%s\", \"%s\") returned error %s\n", oldfilename, newfilename, Explain());
  329.             Where();
  330.         }
  331.     }
  332. }
  333.  
  334. void RdLink(char ch1, char ch2, const char * cmd)
  335. {
  336.     char path[200];
  337.     char link[200];
  338.     int  len;
  339.  
  340.     if (sscanf(cmd, "%s", path) != 1)
  341.         Usage(ch1, ch2);
  342.     
  343.     len = readlink(path, link, 199);
  344.     
  345.     if (len < 0)
  346.         printf("# readlink(\"%s\") returned error %s\n", path, Explain());
  347.     else {
  348.         link[len] = 0;
  349.         printf("# readlink(\"%s\") returned \"%s\"\n", path, link);
  350.     }
  351.         
  352.     Where();
  353. }
  354.  
  355. void Pwd(char ch1, char ch2, const char * cmd)
  356. {
  357.     char * buf;
  358.     
  359.     buf = getcwd(NULL, 1024);
  360.     
  361.     if (!buf)
  362.         printf("# getcwd() returned error %s\n", Explain());
  363.     else {
  364.         printf("# getcwd() returned \"%s\"\n", buf);
  365.         
  366.         free(buf);
  367.     }
  368.         
  369.     Where();
  370. }
  371.  
  372. void Choose(char ch1, char ch2, const char * cmd)
  373. {
  374.     int                flags;
  375.     int                len    =    250;
  376.     char                fType[5];
  377.     char                name[250];
  378.     sa_constr_file    constr;
  379.     
  380.     flags = ((ch1 == 'g') ? 0 : CHOOSE_NEW) | ((ch2 == 'f') ? 0 : CHOOSE_DIR);
  381.  
  382.     if (flags) {
  383.         if (sscanf(cmd, "%s", name) == 1)
  384.             flags |= CHOOSE_DEFAULT;
  385.     } else if (sscanf(cmd, "%s", fType) == 1) {
  386.         constr.numTypes    =    1;
  387.         constr.types[0]    =    *(OSType *) fType;
  388.     } else
  389.         constr.numTypes    =    -1;
  390.     
  391.     if (choose(AF_FILE, 0, "What's up ?", &constr, flags, name, &len))
  392.         printf("# choose(%d) returned error %s\n", flags, Explain());
  393.     else
  394.         printf("# choose(%d) returned \"%s\"\n", flags, name);
  395.         
  396.     Where();
  397. }
  398.  
  399. void Access(char ch1, char ch2, const char * cmd)
  400. {
  401.     char            filename[80];
  402.  
  403.     if (sscanf(cmd, "%s", filename) != 1)
  404.         Usage(ch1, ch2);
  405.     else {
  406.         if (access(filename, F_OK))
  407.             printf("# access(\"%s\", F_OK) returned error %s\n", filename, Explain());
  408.         else
  409.             printf("# access(\"%s\", F_OK) succeeded.\n", filename, Explain());
  410.         if (access(filename, R_OK))
  411.             printf("# access(\"%s\", R_OK) returned error %s\n", filename, Explain());
  412.         else
  413.             printf("# access(\"%s\", R_OK) succeeded.\n", filename, Explain());
  414.         if (access(filename, W_OK))
  415.             printf("# access(\"%s\", W_OK) returned error %s\n", filename, Explain());
  416.         else
  417.             printf("# access(\"%s\", W_OK) succeeded.\n", filename, Explain());
  418.         if (access(filename, X_OK))
  419.             printf("# access(\"%s\", X_OK) returned error %s\n", filename, Explain());
  420.         else
  421.             printf("# access(\"%s\", X_OK) succeeded.\n", filename, Explain());
  422.         Where();
  423.     }
  424. }
  425.  
  426. main(int argc, char ** argv)
  427. {
  428.     GUSISetup(GUSIwithSIOUXSockets);
  429.     
  430.     printf("GUSIFileTest        MN 05Apr95\n\n");
  431.  
  432.     COMMAND('s', 't', Stat,  "filename",             "Call stat() on a file");
  433.     COMMAND('s', 'l', Stat,  "filename",             "Call lstat() on a file");
  434.     COMMAND('c', 'd', ChDir, "directory",             "Call chdir()");
  435.     COMMAND('l', 's', List,  "[ directory ]",     "List a directory");
  436.     COMMAND('l', 'l', List,  "[ directory ]",     "List a directory with more info");
  437.     COMMAND('m', 'd', MkDir, "directory",            "Make a new directory");
  438.     COMMAND('r', 'd', RmDir, "directory",            "Delete an empty directory");
  439.     COMMAND('t', 'y', Type,  "filename",            "Type out the contents of a file");
  440.     COMMAND('e', 'd', Edit,  "filename",            "Enter the contents of a new file");
  441.     COMMAND('m', 'v', Mv,       "oldfile newfile",    "Rename/Move a file");
  442.     COMMAND('r', 'm', Rm,      "filename",            "Delete a file");
  443.     COMMAND('r', 'l', RdLink,"filename",            "Read symbolic link");
  444.     COMMAND('l', 'n', Link,  "oldfile newfile",    "Create a symbolic link");
  445.     COMMAND('g', 'f', Choose,"[ type ]",            "Let the user choose a file");
  446.     COMMAND('g', 'd', Choose,"[ default ]",        "Let the user choose a directory");
  447.     COMMAND('p', 'f', Choose,"[ default ]",        "Let the user create a file");
  448.     COMMAND('p', 'd', Choose,"[ default ]",        "Let the user create a directory");
  449.     COMMAND('p', 'w', Pwd,     "",                        "Print current directory");
  450.     COMMAND('e', 'n', Encode,"filename",            "Translate filename to encoded FSSpec");
  451.     COMMAND('g', 'l', Glob,     "filename",            "Expand wildcards");
  452.     COMMAND('a', 'c', Access,"filename",            "Test access rights");
  453.     
  454.     RunTest(argc, argv);
  455. }